package com.neu.niuyuchao.util;

import org.apache.commons.mail.Email;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.HtmlEmail;

public class EmailUtil {

    public static boolean sendEmail(String toEmail) {
        boolean flag = false;
        //创建一个邮件,内容可以包含 html 标记
        Email email = new HtmlEmail();
        //发件的服务器,从哪个服务器往外发  smtp.126.com  smtp.qq.com
        //smtp 拼接 @ 后面的内容
        email.setHostName("smtp.163.com");
        //设置登录的账号和密码  @前面的是账号 15605350360@163.com
        email.setAuthentication("15610618756", "123456789.");
        email.setCharset("UTF-8");

        try {
            //设置发件人的邮箱
            email.setFrom("15610618756@163.com","三枪吃鸡only秀");
            //设置收件人
            email.addTo(toEmail);
            email.setSubject("登录验证");
            int a = (int)(Math.random()*(9999-1000+1))+1000;
            String msg="欢迎登陆公寓管理系统,验证码"+String.valueOf(a)+",欢迎下次使用。";
            email.setMsg(msg);
            String rst=email.send();
//            System.out.println(rst);
            flag=true;

        } catch (EmailException e) {
            e.printStackTrace();
        }
        return flag;
    }
}
